<form class="layui-form">
    <input type="hidden" name="id" value="${data.id!}">
    <table class="layui-table layui-table-content" lay-skin="nob">
        <tbody>
        <tr>
            <th width="150"><span class="red">*</span>名称</th>
            <td>
                <#if data.id??==false>
                    <input type="text" name="name" placeholder="请输入名称" lay-verify="required" autocomplete="off"
                           class="layui-input" value="${data.name!}">
                    <#else>
                        ${data.name!}
                </#if>
            </td>
        </tr>
        <tr>
            <th>状态</th>
            <td>
                <input type="checkbox" name="status" lay-verify="" value="1" data-val="${data.status!}"
                       lay-skin="switch" lay-text="开启|关闭">
            </td>
        </tr>
        <tr>
            <th>备注</th>
            <td>
                <textarea name="remark" lay-verify="" placeholder="请输入备注"
                          class="layui-textarea">${data.remark!}</textarea>
            </td>
        </tr>
        <tr>
            <th>关联菜单</th>
            <td>
                <ul id="tree" class="ztree"></ul>
            </td>
        </tr>
        </tbody>
    </table>
    <div class="layui-button">
        <button class="layui-btn" style=" min-width: 150px;" lay-submit lay-filter="formSubmit">提 交</button>
        <div class="layui-btn" style=" min-width: 150px;" onclick="location.href='/administrate/role'">返 回</div>
    </div>
</form>
<#macro script>
    <link rel="stylesheet" href="/resources/plugin/ztree/css/metroStyle/metroStyle.css"/>
    <script type="text/javascript" src="/resources/plugin/jquery/jquery-1.7.2.min.js"></script>
    <script type="text/javascript" src="/resources/plugin/ztree/js/jquery.ztree.all.min.js"></script>
    <script>
        var tree = $.fn.zTree.init($("#tree"), {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        }, JSON.parse('${menus}'));
        layui.use(['jquery', 'form', 'layer', 'element'], function () {
            var $ = layui.jquery,
                form = layui.form;
            $(function () {
                if ($("input[name='status']").data("val") == 1) {
                    $("input[name='status']").attr("checked", true);
                }
                else {
                    $("input[name='status']").attr("checked", false);
                }
                form.render('checkbox');
            });
            /*提交*/
            form.on('submit(formSubmit)', function (data) {
                if (data.field.status == null) {
                    data.field.status = 2;
                }
                var menus = new Array();
                $.each(tree.getCheckedNodes(true), function (index, menu) {
                    menus.push(menu.id);
                });
                data.field.menus = menus;
                var url;
                if (data.field.id == "") {
                    url = "/administrate/role/edit/insert";
                } else {
                    url = "/administrate/role/edit/update";
                }
                $.ajax({
                    type: "POST",
                    url: url,
                    data: data.field,
                    async: false,
                    dataType: "json",
                    success: function (result) {
                        if (result.code == 200) {
                            layer.confirm('是否继续编辑', {
                                btn: ['是', '否']
                            }, function () {
                                location.href = location.href;
                            }, function () {
                                location.href = "/administrate/role";
                            });
                        }
                        else {
                            layer.msg(result.msg);
                        }
                    }
                });
                return false;
            });
        });
    </script>
</#macro>